﻿#region 程序集引用
using System;
using System.Web;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Configuration;
using System.Reflection;
using System.IO;
#endregion

#region 版权注释
/*===========================================================================
// Copyright (C) 2017 Zhang.cn，并保留所有 
// 作者：吴岸标 QQ:405403610
// 创建日期：2017-09-03
// 功能描述： 记录分页
===========================================================================*/
#endregion

namespace Zhang
{
    namespace Utils
    {
        public static class SplitStaticPage
        {
            private static readonly string TemplateHtmlCode = "<div class=\"split\"><span class=\"text\">共{$RSTotal}条，{$TotalPage}页</span><a href=\"{$PrevPageUrl}\" class=\"prev\">上一页</a>[ABShop.Loop]<a href=\"{$PageUrl}\">{$PageIndex}</a>[$$$$]<span class=\"current\">{$CurrentPageIndex}</span>[/ABShop.Loop]<a href=\"{$NextPageUrl}\" class=\"next\">下一页</a></div><div class=\"clear\"></div>";
            public static string EnTemplateHtmlCode = "<div class=\"split\"><span class=\"text\">Total:{$RSTotal}，Pages:{$TotalPage}</span><a href=\"{$PrevPageUrl}\" class=\"prev\">Previous</a>[ABShop.Loop]<a href=\"{$PageUrl}\">{$PageIndex}</a>[$$$$]<span class=\"current\">{$CurrentPageIndex}</span>[/ABShop.Loop]<a href=\"{$NextPageUrl}\" class=\"next\">Next</a></div><div class=\"clear\"></div>";
            public static string EnTemplateHtmlCodeWithoutTotal = "<div class=\"split\"><a href=\"{$PrevPageUrl}\" class=\"prev\">Previous</a>[ABShop.Loop]<a href=\"{$PageUrl}\">{$PageIndex}</a>[$$$$]<span class=\"current\">{$CurrentPageIndex}</span>[/ABShop.Loop]<a href=\"{$NextPageUrl}\" class=\"next\">Next</a></div><div class=\"clear\"></div>";
            #region 获取分页HTML代码
            /// <summary>
            /// 获取分页HTML代码
            /// </summary>
            /// <param name="pageName">列表处理url如list.aspx</param>
            /// <param name="midCount">分页中间显示的页码数</param>
            /// <param name="pageIndex">当前页码</param>
            /// <param name="pageSize">分页大小</param>
            /// <param name="rsCount">总记录数</param>
            /// <returns></returns>
            public static string GetHtmlCode(string pageName, int midCount, int pageIndex, int pageSize, Int64 rsCount)
            {
                return CreateHtmlCode(pageName, midCount, pageIndex, pageSize, rsCount, TemplateHtmlCode);
            }
            public static string GetHtmlCode(string pageName, int midCount, int pageIndex, int pageSize, Int64 rsCount, string tempHtmlCode)
            {
                return CreateHtmlCode(pageName, midCount, pageIndex, pageSize, rsCount, tempHtmlCode);
            }
            /// <summary>
            /// 获取分页HTML代码
            /// </summary>
            /// <param name="pageName">列表处理url如list.aspx,当isFuncType=true时为null</param>
            /// <param name="midCount">分页中间显示的页码数</param>
            /// <param name="pageIndex">当前页码</param>
            /// <param name="pageSize">分页大小</param>
            /// <param name="rsCount">总记录数</param>
            /// <param name="tempHtmlCode">分页模板标签  首页URL：{$FirstPageUrl}  尾页URL:{$LastPageUrl} 记录总条数：{$RSTotal}  总页数：{$TotalPage} 上一页URL：{$PrevPageUrl} [ABShop.Loop] 页码对应URL：{$PageUrl}  页码：{$PageIndex} [$$$$] 当前页码：{$CurrentPageIndex} [/ABShop.Loop] 下一页URL：{$NextPageUrl} 
            /// <returns></returns>
            public static string CreateHtmlCode(string pageName, int midCount, int pageIndex, int pageSize, Int64 rsCount, string tempHtmlCode)
            {
                string itemTemp;            // 分页链接及样式
                string currentPageTemp;     // 当前分页链接及样式
                string splitHtmlCode;       // 分页代码
                StringBuilder sbItemHtmlCode; // 所有分页面码链接
                string tempLink;            // 临时变量，保存循环中的当前分页链接
                string[] arrTemp;           // 临时变量 
                Regex reg;
                Match match;
                int midAmount;
                string pageUrl;
                Int64 pageCount;
                string template;

                template = tempHtmlCode;
                itemTemp = string.Empty;
                tempLink = string.Empty;
                currentPageTemp = string.Empty;
                midAmount = midCount;

                reg = new Regex(@"\[ABShop\.Loop\s*(count\s*=\s*[""]\d+[""])?\s*\](?<1>.+?)\[/ABShop\.Loop\]", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                match = reg.Match(template);
                splitHtmlCode = template;
                splitHtmlCode = splitHtmlCode.Replace("(pagesize)", pageSize.ToString());
                pageUrl = pageName;

                if (rsCount < pageSize)
                {
                    pageCount = 1;
                }
                else
                {
                    if (rsCount % pageSize == 0)
                    {
                        pageCount = rsCount / pageSize;
                    }
                    else
                    {
                        pageCount = rsCount / pageSize + 1;
                    }
                }

                if (midAmount == 0)
                {
                    midAmount = 8;
                }

                splitHtmlCode = SetLabeslValue(splitHtmlCode, pageIndex, pageCount, pageSize, rsCount, pageUrl);

                if (match.Success)      // 分页循环标签
                {
                    itemTemp = match.Groups[1].Value;
                    splitHtmlCode = splitHtmlCode.Replace(match.Value, "{[#SplitItem#]}");

                    if (itemTemp.Contains("[$$$$]"))    // 存在当前分页链接样式
                    {
                        arrTemp = itemTemp.Split(new string[] { "[$$$$]" }, StringSplitOptions.None);

                        if (arrTemp.Length > 1)
                        {
                            itemTemp = arrTemp[0];
                            currentPageTemp = arrTemp[1];
                        }
                        else
                        {
                            itemTemp = arrTemp[0];
                            currentPageTemp = arrTemp[0];
                        }
                    }

                    if (pageCount > midAmount + 6)
                    {
                        if (pageIndex > midAmount / 2 + 2)
                        {
                            if (pageIndex > (pageCount - (midAmount / 2) - 2))
                            {
                                sbItemHtmlCode = GetIntermittentHtmlCode(pageUrl, itemTemp, currentPageTemp, pageIndex, pageCount - midAmount, pageCount, true, false, pageCount, rsCount);
                            }
                            else
                            {
                                sbItemHtmlCode = GetIntermittentHtmlCode(pageUrl, itemTemp, currentPageTemp, pageIndex, pageIndex - midAmount / 2, pageIndex + midAmount / 2, true, true, pageCount, rsCount);
                            }
                        }
                        else
                        {
                            sbItemHtmlCode = GetIntermittentHtmlCode(pageUrl, itemTemp, currentPageTemp, pageIndex, 3, midAmount + 2, false, true, pageCount, rsCount);
                        }

                    }
                    else
                    {
                        sbItemHtmlCode = GetIntermittentHtmlCode(pageUrl, itemTemp, currentPageTemp, pageIndex, 3, pageCount, false, false, pageCount, rsCount);
                    }


                    splitHtmlCode = splitHtmlCode.Replace("{[#SplitItem#]}", sbItemHtmlCode.ToString());
                }

                splitHtmlCode = splitHtmlCode.Replace("{$CurrentPageIndex}", pageIndex.ToString());
                return splitHtmlCode;
            }
            #endregion

            #region 中间分页链接
            private static StringBuilder GetIntermittentHtmlCode(string pageUrl, string itemTemp, string currentPageTemp, Int64 currentPageIndex, Int64 startIndex, Int64 endIndex, bool isStart, bool isEnd, Int64 pageCount, Int64 totalRS)
            {
                StringBuilder sbItemHtmlCode;
                string tempLink;
                string firstPageUrl;

                sbItemHtmlCode = new StringBuilder();
                firstPageUrl = pageUrl.Replace("{pageIndex}", "1");


                if (currentPageIndex != 1)
                {
                    tempLink = itemTemp.Replace("{$PageUrl}", firstPageUrl);  // 第一页
                    tempLink = tempLink.Replace("{$PageIndex}", "1");
                }
                else  // 当前页为第一页
                {
                    tempLink = currentPageTemp.Replace("{$CurrentPageUrl}", firstPageUrl);  // 第一页
                }

                sbItemHtmlCode.Append(tempLink);

                if (pageCount > 1)
                {
                    if (currentPageIndex != 2)
                    {
                        tempLink = itemTemp.Replace("{$PageUrl}", pageUrl.Replace("{pageIndex}", "2"));  // 第二页
                        tempLink = tempLink.Replace("{$PageIndex}", "2");
                    }
                    else  // 当前页为第二页
                    {
                        tempLink = currentPageTemp.Replace("{$CurrentPageUrl}", pageUrl.Replace("{pageIndex}", "2"));  // 第二页
                        tempLink = tempLink.Replace("{$CurrentPageIndex}", "2");
                    }

                    sbItemHtmlCode.Append(tempLink);
                }

                if (isStart)
                {
                    sbItemHtmlCode.Append("<span class=\"text\">...</span> ");
                }

                for (Int64 k = startIndex; k <= endIndex; k++)
                {
                    if (k == currentPageIndex)
                    {
                        tempLink = currentPageTemp.Replace("{$CurrentPageUrl}", pageUrl.Replace("{pageIndex}", k.ToString()));
                        tempLink = tempLink.Replace("{$CurrentPageIndex}", k.ToString());
                    }
                    else
                    {
                        tempLink = itemTemp.Replace("{$PageUrl}", pageUrl.Replace("{pageIndex}", k.ToString()));
                        tempLink = tempLink.Replace("{$PageIndex}", k.ToString());
                    }

                    sbItemHtmlCode.Append(tempLink);
                }

                if (isEnd)
                {
                    sbItemHtmlCode.Append("<span class=\"text\">...</span> ");
                    tempLink = itemTemp.Replace("{$PageUrl}", pageUrl.Replace("{pageIndex}", (pageCount - 1).ToString()));
                    tempLink = tempLink.Replace("{$PageIndex}", (pageCount - 1).ToString());
                    sbItemHtmlCode.Append(tempLink);
                    tempLink = itemTemp.Replace("{$PageUrl}", pageUrl.Replace("{pageIndex}", pageCount.ToString()));
                    tempLink = tempLink.Replace("{$PageIndex}", pageCount.ToString());
                    sbItemHtmlCode.Append(tempLink);
                }

                return sbItemHtmlCode;
            }
            #endregion

            #region 替换分页标签中的变量
            private static string SetLabeslValue(string tempContent, int currentPageIndex, Int64 totalPage, int pageSize, Int64 totalRS, string pageUrl)
            {
                string resultContent;
                string firstPageUrl;

                resultContent = tempContent;
                pageUrl = pageUrl.Trim();
                firstPageUrl = pageUrl;
                firstPageUrl = firstPageUrl.Replace("{pageIndex}", "1");

                if (currentPageIndex == 1)
                {
                    resultContent = resultContent.Replace("class=\"prev\"", "class=\"prev-disabled\"");
                    resultContent = resultContent.Replace("{$FirstPageUrl}", "javascript:void(0)");
                    resultContent = resultContent.Replace("{$PrevPageUrl}", "javascript:void(0)");
                }

                if (currentPageIndex == totalPage)
                {
                    resultContent = resultContent.Replace("class=\"next\"", "class=\"next-disabled\"");
                    resultContent = resultContent.Replace("{$NextPageUrl}", "javascript:void(0)");
                    resultContent = resultContent.Replace("{$LastPageUrl}", "javascript:void(0)");
                }

                resultContent = resultContent.Replace("{$TotalPage}", totalPage.ToString());                       // 总页数
                resultContent = resultContent.Replace("{$FirstPageUrl}", firstPageUrl);                            // 首页地址
                resultContent = resultContent.Replace("{$RSTotal}", totalRS.ToString());                           // 记录总数
                resultContent = resultContent.Replace("{$PageSize}", pageSize.ToString());                         // 分页大小

                if (currentPageIndex > 1)
                {
                    resultContent = resultContent.Replace("{$PrevIndex}", (currentPageIndex - 1).ToString());       // 上一页
                }
                else
                {
                    resultContent = resultContent.Replace("{$PrevIndex}", "1");                                      // 上一页
                }

                if (currentPageIndex >= totalPage)
                {
                    resultContent = resultContent.Replace("{$NextIndex}", totalPage.ToString());                     // 下一页
                }
                else
                {
                    resultContent = resultContent.Replace("{$NextIndex}", (currentPageIndex + 1).ToString());        // 下一页
                }

                if (currentPageIndex > 2)   // 上一页地址
                {
                    resultContent = resultContent.Replace("{$PrevPageUrl}", pageUrl.Replace("{pageIndex}", (currentPageIndex - 1).ToString()));
                }
                else
                {
                    resultContent = resultContent.Replace("{$PrevPageUrl}", firstPageUrl);
                }

                if (totalPage - 1 > currentPageIndex)  // 下一页地址
                {
                    resultContent = resultContent.Replace("{$NextPageUrl}", pageUrl.Replace("{pageIndex}", (currentPageIndex + 1).ToString()));
                }
                else
                {
                    if (totalPage != 1)
                    {
                        resultContent = resultContent.Replace("{$NextPageUrl}", pageUrl.Replace("{pageIndex}", totalPage.ToString()));
                    }
                    else
                    {
                        resultContent = resultContent.Replace("{$NextPageUrl}", firstPageUrl);
                    }
                }

                if (totalPage != 1) // 最后一页地址
                {
                    resultContent = resultContent.Replace("{$LastPageUrl}", pageUrl.Replace("{pageIndex}", totalPage.ToString()));
                }
                else
                {
                    resultContent = resultContent.Replace("{$LastPageUrl}", firstPageUrl);
                }

                return resultContent;
            }
            #endregion
        }
    }
}
